package com.cmfi.controller.PlcController;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cmfi.domain.PlcEntity.Plc;
import com.cmfi.service.inter.PlcInter.PlcService;
import com.cmfi.utils.Code;
import com.cmfi.utils.Result;

@RestController
@RequestMapping("/api")
public class PlcController {

    private final Logger log = LoggerFactory.getLogger(PlcController.class);
    @Autowired
    private PlcService plcService;

    @PostMapping("/plc")
    public Result addPlc(@RequestBody Plc plc) {
        try {
        	plcService.addPlc(plc);
    		return new Result(Code.SAVE_OK, "");
    	} catch (Exception e) {
    		log.error("[PlcController][addPlc]  e,", e);
    		return new Result(Code.SAVE_ERR, e.getMessage());
    	}
    }

	@DeleteMapping("/plc/{plcNum}")
	public Result delPlc(@PathVariable(value = "plcNum", required = false) final String plcNum) {
		try {
			plcService.delPlcByPlcNum(Long.valueOf(plcNum));
			return new Result(Code.DELETE_OK, plcNum);
		} catch (Exception e) {
			log.error("[PlcController][delPlc]  e,", e);
			return new Result(Code.DELETE_OK, e.getMessage());
		}
    }
	
	
	/**
	 * {@code GET  /forklift-qualities} : get all the forkliftQualities.
	 *
	 * @param pageable the pagination information.
	 * @param request  a {@link ServerHttpRequest} request.
	 * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list
	 *         of forkliftQualities in body.
	 */
	@GetMapping("/plcs")
	public Result getAllPlcs() {
		try {
			List<Plc> plcList = plcService.getAllPlc();
			return new Result(Code.GET_OK, plcList);
		} catch (Exception e) {
			log.error("[PlcController][getAllRoles] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
	}
}
